package com.slipper.system.codeless.controller;

import com.slipper.system.codeless.bo.DataFieldRelevanceBO;
import com.slipper.system.codeless.service.DataFieldRelevanceService;
import com.slipper.system.dictionary.service.DictionaryDataService;
import com.slipper.util.Tool;
import jakarta.annotation.Resource;

import com.slipper.constants.Constants;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.DataFieldBO;
import com.slipper.system.codeless.service.DataFieldService;
import com.slipper.util.ResultUtil;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * 数据字段信息表
 */
@RestController
@RequestMapping("/system/dataField")
public class DataFieldController {
    @Resource
    private DataFieldService dataFieldService;
    @Resource
    private DataFieldRelevanceService dataFieldRelevanceService;
    @Resource
    private DictionaryDataService dictionaryDataService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody DataFieldBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(dataFieldService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody DataFieldBO bo) {
        List<DataFieldBO> dataFieldList = dataFieldService.queryList(bo);
        dataFieldList.forEach(dataField -> {
            DataFieldRelevanceBO fieldRelevanceQO = new DataFieldRelevanceBO();
            fieldRelevanceQO.setFieldId(dataField.getId());
            dataField.setRelevanceList(dataFieldRelevanceService.queryList(fieldRelevanceQO));
        });
        return ResultUtil.successResult(dataFieldList);
    }

    @PostMapping(value = "/queryDictList")
    public String queryDictList(@RequestBody DataFieldBO bo) {
        List<Map<String, String>> resultList = new ArrayList<>();
        if (Tool.isBlank(bo.getFieldName())) return ResultUtil.successResult(resultList);
        DataFieldBO dataFieldBO = dataFieldService.queryByFieldName(bo.getModelId(), bo.getFieldName());
        if (dataFieldBO == null) return ResultUtil.successResult(resultList);
        if (DataFieldBO.FIELD_TYPE_VARCHAR.equals(dataFieldBO.getFieldType())
                && Constants.YES.equals(dataFieldBO.getEnumerate())
                && Tool.isNotBlank(dataFieldBO.getEnumerateValue())) {
            String[] enumerateValues = dataFieldBO.getEnumerateValue().split(",");
            for (String enumerateValue : enumerateValues) {
                resultList.add(new HashMap<>() {{
                    put("key", enumerateValue);
                    put("value", enumerateValue);
                }});
            }
        } else if (DataFieldBO.FIELD_TYPE_RELATION.equals(dataFieldBO.getFieldType())
                && Tool.isNotBlank(dataFieldBO.getRelevanceType())
                && dataFieldBO.getRelevanceType().startsWith("字典")) {
            dictionaryDataService.queryByDictId(dataFieldBO.getRelevanceDict()).forEach(data ->
                    resultList.add(new HashMap<>() {{
                        put("key", data.getDictKey());
                        put("value", data.getDictValue());
                    }}));
        }
        return ResultUtil.successResult(resultList);
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody DataFieldBO bo) {
        return ResultUtil.successResult(dataFieldService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody DataFieldBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(dataFieldService.save(bo));
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody DataFieldBO bo) {
        return ResultUtil.deleteResult(dataFieldService.delete(bo));
    }
}
